﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
namespace ActionSystem.Core.AnimationLogic
{
    public abstract class BaseAnimationAbility
    {
        BaseStateMachine _stateMachine;
        public BaseStateMachine StateMachine=>_stateMachine;
        public BaseAnimationAbility(BaseStateMachine baseStateMachine)
        {
            _stateMachine = baseStateMachine;
        }
        public virtual void BeforeIKEvaluate()
        {
            //执行当前状态的逻辑
            var currentState = _stateMachine.CurrentState;
            var currentTime = _stateMachine.CurrentStateData;
            if (currentState == null)
            {
                return;
            }
            foreach (var action in currentState.GetActions())
            {
                if (action.IsExcuting)
                    (action.Handler as IAnimationActionHandler)?.BeforeIKEvaluate(action, currentTime.logicDeltaTime);
            }

            //判断是否需要执行下一个状态
            var nextState = _stateMachine.NextState;
            if (nextState == null)
            {
                return;
            }
            var nextTime = _stateMachine.NextStateData;

            foreach (var action in nextState.GetActions())
            {
                if (action.IsExcuting)
                    (action.Handler as IAnimationActionHandler)?.BeforeIKEvaluate(action, nextTime.logicDeltaTime);
            }


        }
        public virtual void BeforeAnimationEvaluate() 
        {
            //执行当前状态的逻辑
            var currentState = _stateMachine.CurrentState;
            var currentTime = _stateMachine.CurrentStateData;
            if(currentState==null)
            {
                return;
            }
            foreach (var action in currentState.GetActions())
            {
                if (action.IsExcuting)
                    (action.Handler as IAnimationActionHandler)?.BeforeAnimationEvaluate(action, currentTime.logicDeltaTime);
            }
                           
            //判断是否需要执行下一个状态
            var nextState = _stateMachine.NextState;
            if (nextState==null)
            {
                return;
            }
            var nextTime = _stateMachine.NextStateData;
          
            foreach (var action in nextState.GetActions())
            {
                if (action.IsExcuting)
                    (action.Handler as IAnimationActionHandler)?.BeforeAnimationEvaluate(action, nextTime.logicDeltaTime);
            }
            
            
        }
        public abstract void AnimationEvaluate();
        public virtual void AfterAnimationEvaluate() 
        {
            //执行当前状态的逻辑
            var currentState = _stateMachine.CurrentState;
            var currentTime = _stateMachine.CurrentStateData;
            if (currentState == null)
            {
                return;
            }
            foreach (var action in currentState.GetActions())
            {
                if (action.IsExcuting)
                    (action.Handler as IAnimationActionHandler)?.AfterAnimationEvaluate(action, currentTime.logicDeltaTime);
            }
            
                
            //判断是否需要执行下一个状态
            var nextState = _stateMachine.NextState;
            if (nextState == null)
            {
                return;
            }
            var nextTime = _stateMachine.NextStateData;

            foreach (var action in nextState.GetActions())
            {
                if (action.IsExcuting)
                    (action.Handler as IAnimationActionHandler)?.AfterAnimationEvaluate(action, nextTime.logicDeltaTime);
            }            
        }
        public virtual void IKSolve() { }
    }
}
